/*这个程序用于测试6.25Mhz版本的六轴版本*/
/*但是这个是函数版本，把这些都写成了基本的函数。*/
/*tap=0,1,2,3,4,5,6,7,8.对应6.25Mhz,3.125Mhz~24.414khz*/
/*写地址描述(就是赋值给addr_reg[5:0])
0-1:11'vel0
2-3:11'vel1
4-5:11'vel2
6-7:11'vel3
8-9:11'vel4
10-11:11'vel5

12-13:4'tap0+12'steptime0
14-15:12'dirtime0
16-17:4'tap1+12'steptime1
18-19:12'dirtime1
20-21:4'tap2+12'steptime2
22-23:12'dirtime2
24-25:4'tap3+12'steptime3
26-27:12'dirtime3
28-29:4'tap4+12'steptime4
30-31:12'dirtime4
32-33:4'tap5+12'steptime5
34-35:12'dirtime5


*/

#include <stdio.h>  
#include <sys/io.h> 
#include <unistd.h>
#define setbit(x,y) x|=(1<<y)
#define clrbit(x,y) x&=~(1<<y)
int BASE=0x378;//0x378
//这个函数旨在给FPGA传送16位addvel（stepgen.c语）
void write16addvel(unsigned int w, int i) {
	int add_reg;
	add_reg=2*i;//根据与fpga的约定
	outb(add_reg,BASE+3);
	outb(w & 0xff, BASE+4);
	add_reg=2*i+1;//根据与fpga的约定
	outb(add_reg,BASE+3);
    outb(w >> 8, BASE+4);
}
//这个函数旨在传递steplen_ticks,dirtime_ticks,还有tap,另外i表示0,1,2关节
void write32ticks_tap(unsigned int w,int i){
	int add_reg;
	//写12'steptimeX,4'tapX
	add_reg=4*i+12;//根据与fpga的约定
	outb(add_reg,BASE+3);
	outb(w & 0xff, BASE+4);//先写个8位过去
	add_reg=4*i+13;//根据与fpga的约定
	outb(add_reg,BASE+3);
    	outb(w >> 8, BASE+4);//再写个8位过去
	//写12'dirtimeX
	add_reg=4*i+14;//根据与fpga的约定
        outb(add_reg,BASE+3);
        outb(w >> 16, BASE+4);
        add_reg=4*i+15;//根据与fpga的约定
        outb(add_reg,BASE+3);
        outb(w >> 24, BASE+4);

}
int read32pos(void) {
	int add_reg;
    unsigned char a, b, c, d;

	add_reg=0x00;
	outb(add_reg,BASE+3);
	a=inb(BASE+4);

	add_reg=0x01;
	outb(add_reg,BASE+3);
	b=inb(BASE+4);

	add_reg=0x02;
	outb(add_reg,BASE+3);
	c=inb(BASE+4);

	add_reg=0x03;
	outb(add_reg,BASE+3);
	d=inb(BASE+4);

    return a + (b<<8) + (c<<16) + (d<<24);
}

/**************************************************/
int main(unsigned char data)
{
unsigned char x;
int a,b,c,d,y;
int statusport = BASE + 1;
int controlport = BASE + 2;
unsigned char status,control;
unsigned char ctl;
//char x;
iopl(3);
ioperm(BASE,0x4ff,1);          
unsigned char EPP_ECR = 0x80;//0x80epp
unsigned char old_ECR = inb_p(BASE + 0x402);/* Paused IO for example */
unsigned char low_bits = old_ECR & 0x1f; /* save 5 lowest bits */ 
unsigned char new_ECR = EPP_ECR | low_bits;
outb_p(new_ECR, BASE + 0x402); /* set the port to EPP */


ctl = inb(controlport);
ctl = (ctl &= 0xF0) | 0x4;
outb(ctl, controlport);

ctl= inb(controlport);
setbit(ctl,5);//setbit(ctl,5)是正确的
outb(ctl,controlport);


if ((inb(statusport) & 0x01)) {//if the bit0 ==1,means TIME OUT
  status = inb(statusport); 
  outb(status | 0x01, statusport); /* Some reset by writing 1 */ 
  outb(status & 0xfe, statusport); /* Others by writing 0 */
}
//define vel0
	write32ticks_tap(0x00010001,0);//tap0=0,steptime=1,dirtime=1;
//	write32ticks_tap(0x00031003,0);//tap0=0,steptime=1,dirtime=1;
	write32ticks_tap(0x000c700c,1);//tap0=0,steptime=1,dirtime=1;
	//write32ticks_tap(0x000c800c,2);//tap0=0,steptime=1,dirtime=1;
	write32ticks_tap(0x00010001,2);//tap0=0,steptime=1,dirtime=1;
while(1){
	write16addvel(0x50,0);//这样结合上面设置，预判输出最高频率6.25Mhz
	write16addvel(0x400,1);
	write16addvel(0x400,2);
/////////////////////////////
printf("pos0=%x\n",read32pos());

usleep(1000);
}

x=inb(BASE+4);
printf("x=%x\n",x);

ioperm(BASE,0x4ff,0);  
return 0;
}  
